package com.sdd.rpi_mobile.homeScreen;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdd.rpi_mobile.R;
import com.sdd.rpi_mobile.eventRss.RssFeedChooserActivity;
import com.sdd.rpi_mobile.resourceLists.ListActivity;

/**
 * Adapter for a list of Views that contain a TextView and an image.
 * The image adapter is used to setup the view and get the intent for a given activity. 
 */
public class ImageAdapter extends BaseAdapter {
	
	private Context mContext;
	
	private Feature[] mActivityList = {
			new Feature(R.drawable.campus, "Campus", ListActivity.class),
			new Feature(R.drawable.calendar, "Events", RssFeedChooserActivity.class),
			new Feature(R.drawable.map, "Places", ListActivity.class),
			new Feature(R.drawable.useful_info, "Useful Info", ListActivity.class), 
			new Feature(R.drawable.bus_info, "Getting around", ListActivity.class),
			new Feature(R.drawable.transmission, "Getting out", ListActivity.class),
	};
	 
	public ImageAdapter(Context c) {
		mContext= c;
	}

	@Override
	public int getCount() {
		return mActivityList.length;
	}

	@Override
	public Object getItem(int pos) {
		return mActivityList[pos];
	}
	
	/**
	 * Gets the intent of the item specified.
	 * @param pos
	 * Position of the item
	 * @return
	 * Intent of activity where feature is rooted.
	 */
	public Intent getIntent(int pos){
		Intent redirect = new Intent(mContext, mActivityList[pos].getActivity());
		return redirect;
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	/**
	 * Setting up the view of the main window by placing the icons at the right locations.
	 * @param pos
	 * Position of item in data source
	 * @param convertView
	 * this method can create a new view. Heterogeneous lists can specify their number of 
	 * view types, so that this View is always of the right type (see getViewTypeCount() 
	 * and getItemViewType(int)).
	 * @param parent
	 * The parent that this view will eventually be attached to
	 * @return
	 * Returns the icon for the item at pos
	 */
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		LinearLayout icon;
		if(convertView == null) //If the icon has not been created yet
		{
			//Creates a new layout to hold the contents of the icon
			icon = new LinearLayout(mContext);
			icon.setOrientation(LinearLayout.VERTICAL);
			
			//Add label to icon and format it
			TextView text = new TextView(mContext);
			text.setText(mActivityList[pos].getName());
			text.setGravity(Gravity.CENTER);
			text.setTextSize(20);
			icon.addView(text);
			
			//Get the image to be shown and add it to the icon
			ImageView imageView = new ImageView(mContext);
			imageView.setImageResource(mActivityList[pos].getResID());
			imageView.setScaleType(ScaleType.FIT_CENTER);
			icon.addView(imageView);
			
			WindowManager w = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
			int screenW = 0;
			int screenH = 0;		
			
			//Get display size to determine the size of icons
			Point size = new Point();
			Log.v("Exception", "Version number " + Build.VERSION.SDK_INT + " Honeycomb is " + Build.VERSION_CODES.HONEYCOMB);
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ //If Android version is Android 3 or higher
				Log.v("ImageAdapter", "Android 3.0 or higher");
				w.getDefaultDisplay().getSize(size);
				screenW = size.x;
				screenH = size.y-150;
				icon.setLayoutParams(new GridView.LayoutParams((int) (screenW/2.5), screenH/3));
			} else { //If Android version is less than 3.0
				Log.v("ImageAdapter", "Android version <3.0");
				Display d = w.getDefaultDisplay();
				screenW = d.getWidth();
				screenH = d.getHeight();
				imageView.setMaxWidth((int) (screenW/2.5));
				imageView.setMaxHeight(screenH/4);
				icon.setLayoutParams(new GridView.LayoutParams((int) (screenW/2.5), screenH/4));
			}
			icon.setPadding(5, 0, 10, 0);
		}
		else { //If the icon has already been created, use it
			icon = (LinearLayout) convertView;
		}
		return icon;
	}
}
